<?php
class pettycash_model extends CI_Model{
	private $Kodepettycash;
	private $namapettycash;
    private $saldo;
    private $Createdby;
    private $Createdon;
    private $Lastupdateby;
  	private $Lastupdateon;
  	private $Active;
	
	function setKodepettycash($kdpetty){
		$this->Kodepettycash = $kdpetty;
	}
	function getKodepettycash(){
		return $this->Kodepettycash;
	}
	
	function setnamapettycash($bname){
		$this->namapettycash = $bname;
	}
	function getnamapettycash(){
		return $this->namapettycash;
	}
	
	function setsaldo($saldo){
		$this->saldo = $saldo;
	}
	function getsaldo(){
		return $this->saldo;
	}
	
	function setCreatedBy($c_by){
		$this->CreatedBy = $c_by;
	}
	function getCreatedBy(){
		return $this->CreatedBy;
	}

	function setCreatedOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->Createdon = $dateNow;
	}
	function getCreatedOn(){
		return $this->Createdon;
	}

	function setLastUpdateBy($u_by){
		$this->Lastupdateby = $u_by;
	}
	function getLastUpdateBy(){
		return $this->Lastupdateby;
	}

	function setLastUpdateOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->Lastupdateon = $dateNow;
	}
	function getLastUpdateOn(){
		return $this->Lastupdateon;
	}

	function setActive($active_val){
		$this->Active = $active_val;
	}
	function getActive(){
		return $this->Active;
	}
	
	// Call the module constructor
	function __construct(){
		parent::__construct();
	}
	
	function getpettyCode()
	{
		$query = $this->db->query("select MAX(RIGHT(Kodepettycash,3)) as kdpetty from master_petty_cash");
		$kd = "";
		
		if($query-> num_rows() > 0)
		{
			foreach($query->result() as $idpetty)
			{
				$tmppetty = ((int)$idpetty->kdpetty)+1;
				$kd = sprintf("%03s", $tmppetty);
			}
			
		}
		else
		{
			$kd = "001";
		}
		
		$kd = "KP".$kd; 
		
		return $kd;
	}
	
	function insert()
	{
		$query = $this->db->query("select MAX(RIGHT(Kodepettycash,3)) as kdpetty from master_petty_cash");
		$kd = "";
		
		if($query-> num_rows() > 0)
		{
			foreach($query->result() as $idpetty)
			{
				$tmppetty = ((int)$idpetty->kdpetty)+1;
				$kd = sprintf("%03s", $tmppetty);
			}
			
		}
		else
		{
			$kd = "001";
		}
			
		$kdpettyok="KP".$kd;
		$this->setKodepettycash($kdpettyok);
		
		$data = array(
			'Kodepettycash' => $this->getKodepettycash(),
			'namapettycash' => $this->getnamapettycash(),
			'saldo' => $this->getsaldo(),
			'CreatedBy' => $this->getCreatedBy(),
			'CreatedOn' => $this->getCreatedOn(),
			'LastUpdateBy' => 'null',
			'lastupdateon' => 'null',
			'active' => '1'					
		);
		$this->db->insert('master_petty_cash',$data);
	}
	
	function ubahmasterpetty() 
	{
            $data = array(
                'namapettycash' => $this->getnamapettycash(),
				'saldo' => $this->getsaldo(),
                'LastUpdateBy' => $this->getLastUpdateBy(),
                'LastUpdateOn' => $this->getLastUpdateOn()
            );
 
        $this->db->where(array('Kodepettycash' => $this->getKodepettycash()));
        $this->db->update('master_petty_cash', $data);
    }

	function ubahsaldopetty() 
	{
            $data = array(
				'saldo' => $this->getsaldo()
            );
 
        $this->db->where(array('Kodepettycash' => $this->getKodepettycash()));
        $this->db->update('master_petty_cash', $data);
    }
	
    function deletemasterpetty() {
        $data = array(
            'active' => '0'
        );
        $this->db->where(array('Kodepettycash' => $this->getKodepettycash()));
        $this->db->update('master_petty_cash', $data);
    }
	
	function getList(){
		$this->db->select()->where(array('active'=>'1'));
		$query = $this->db->get('master_petty_cash');
		return $query->result();
	}
	
	function countGetList(){
		$this->db->select()->where(array('active'=>'1'));
		$query = $this->db->get('master_petty_cash');
		return $query->num_rows();
	}
	
	function getListlimit($limit,$start)
	{
		$this->db->select()
		->where(array('active'=>'1'))
		->limit($limit,$start);
		$query = $this->db->get('master_petty_cash');
		return $query->result();
	}
	
	function getkdpettycash(){
		$this->db->select()->where(array('Kodepettycash' => $this->getKodepettycash(),'active'=>'1'));
		$query = $this->db->get('master_petty_cash');
		return $query->result();
	}
	
	function getkdpetty(){
		$this->db->select()->where(array('active'=>'1'))->like(array('Kodepettycash' => $this->getKodepettycash()));
		$query = $this->db->get('master_petty_cash');
		return $query->result();
	}
	
	function getnamapetty(){
		$this->db->select()->where(array('active'=>'1'))->like(array('namapettycash' => $this->getnamapettycash()));
		$query = $this->db->get('master_petty_cash');
		return $query->result();
	}
	
	function getTotalsaldo()
	{
		
		$query = $this->db->query("select saldo as totalsaldo from master_petty_cash
			where Kodepettycash='". $this->getKodepettycash() ."'");
		$row = $query->row();
		return $row->totalsaldo;
	}
}